package com.kgc.xsProvider.service.ServiceImpl;

import com.kgc.basic.util.ResponseMsg;
import com.kgc.xs.pojo.SalesCounter;
import com.kgc.xsProvider.dao.SalesCounterMapper;
import com.kgc.xsProvider.service.SalesCounterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;


@Service
@Transactional
public class SalesCounterServiceImpl implements SalesCounterService {

    @Autowired
    private SalesCounterMapper salesCounterMapper;

    private Logger logger = LoggerFactory.getLogger(SalesCounterServiceImpl.class);


    @Override
    public ResponseMsg getCounterByTypeAndDate(Map<String, Object> map) {
        logger.info("getCounterByTypeAndDate   getCounterByTypeAndDate params map {}",map.toString());
        ResponseMsg responseMsg = new ResponseMsg();
        String docType=(String) map.get("docType");
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
        String dateStr=sdf.format(date);
        map.put("dateStr",dateStr);
        SalesCounter salesCounter=salesCounterMapper.getCounterByTypeAndDate(map);
        if (salesCounter == null) {
            salesCounter = new SalesCounter(docType, dateStr, 1);
            salesCounterMapper.insertCounter(salesCounter);

        } else {
            salesCounterMapper.incrementSeq(map);
        }
        String code= String.format("%s-%s-%03d",
                docType, // 获取类型前缀
                dateStr,
                salesCounter.getSeq()
        );
        responseMsg.setData(code);
        return responseMsg;
    }

    @Override
    public ResponseMsg insertCounter(SalesCounter docCounter) {
        return null;
    }

    @Override
    public ResponseMsg updateCounter(Map<String, Object> map) {
        return null;
    }
}
